﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;
using System.Text;

namespace WebSite.Phone
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!Page.IsPostBack)
                {
                    BindBlogType();
                    LoadContent();
                }
            }
        }

        #region 加载类别
        private void BindBlogType()
        {
            BlogTypeHelper bthelper = new BlogTypeHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.Equals, "BlogBigType", "图文");
            DataTable dtBlogType = bthelper.QueryBlogTypeBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, 0, 10, null);

            rptBlogType1.DataSource = dtBlogType;
            rptBlogType1.DataBind();

            rptBlogType2.DataSource = dtBlogType;
            rptBlogType2.DataBind();
        }
        #endregion

        #region 加载内容
        private void LoadContent()
        {
            StringBuilder sbContent = new StringBuilder();
            //加载系统类别
            BlogTypeHelper bthelper = new BlogTypeHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.Equals, "BlogBigType", "图文");


            DataTable dtBlogType = bthelper.QueryBlogTypeBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, 0, 0, null);
            if (dtBlogType != null && dtBlogType.Rows.Count > 0)
            {
                foreach (DataRow drblogtype in dtBlogType.Rows)
                {
                    string blogtypeid = drblogtype["ID"] + "";
                    string blogtypename = drblogtype["minBlogTypeName"] + "";
                    //加载该类别下的博客
                    BlogHelper bhelper = new BlogHelper();
                    Criteria cblog = new Criteria();
                    cblog.Add(CriteriaType.Equals, "BlogTypeID", blogtypeid);
                    cblog.Add(CriteriaType.Equals, "Flag", 0);

                    string[] fields = new string[] { "ID", "BlogBigType", "Title", "HasPic", "Flag", "CreateDate" };
                    DataTable dtBlog = bhelper.QueryBlogBySearchToTable(cblog, new Order[] { new Order("CreateDate", OrderMode.Desc) }, 0, 10, fields);
                    if (dtBlog != null && dtBlog.Rows.Count > 0)
                    {
                        sbContent.Append("<div class=\"listmate\">");
                        sbContent.Append("<strong><img src='../images/4789cfcc.png' alt='' />" + blogtypename + "</strong>");
                        sbContent.Append("</div>");

                        sbContent.Append("<div class=\"matebox\">");
                        sbContent.Append("<div class=\"listbox\">");
                        
                        foreach (DataRow drblog in dtBlog.Rows)
                        {
                            string blogid = drblog["ID"] + "";
                            string title = drblog["Title"] + "";
                            string strtitle = "";
                            strtitle = Tools.GetFirstString(title, 32);
                            //判断是否存在图片
                            string haspic = drblog["haspic"] + "";
                            if (haspic == "1")
                            {
                                strtitle += "(图)";
                            }

                            sbContent.Append("<p><a href=\"Detail.aspx?id=" + blogid + "\">" + strtitle + "</a></p>");
                        }
                        sbContent.Append("</div>");
                        sbContent.Append("</div>");
                        sbContent.Append("");
                    }
                }
            }

            litContent.Text = sbContent.ToString();
        }
        #endregion
    }
}